package com.imageprocess;

import java.io.IOException;
import java.io.InputStream;

import com.imageprocess.demo.WaterView;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private WaterView bitmapView = null;
	private ImageView im;
	private Bitmap bmp;
	private static JNI jni = new JNI();
	static {
		System.loadLibrary("mtimage-jni");
		System.loadLibrary("ImageProcessing");

	}

	public native void me(int[] paramArrayOfInt1, int[] paramArrayOfInt2,
			int paramInt1, int paramInt2);

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏

		// bitmapView = new WaterView(this);
		// this.setContentView(bitmapView);
		BitmapFactory.Options option = new BitmapFactory.Options();
		option.inPreferredConfig = Config.ARGB_8888;
		bmp = (BitmapFactory.decodeResource(this.getResources(),
				R.drawable.demopng, option));
		im = new ImageView(this);

		im.setImageBitmap(bmp);
		this.setContentView(im);
	}

	public static native void renderBmp(Bitmap bitmap);

	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_SPACE:
			Log.d("Filter", "空格");
			Old();
			break;
		case KeyEvent.KEYCODE_0:
			Lomo();
			break;
		case KeyEvent.KEYCODE_1:
			LomoHaolo();
			break;
		case KeyEvent.KEYCODE_3:
			LomoHDR();
			break;
		case KeyEvent.KEYCODE_4:
			LomoB();
			break;
		}

		return true;
	}

	public void Old() {
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("style/mtxx_lzp_b.jpg",
				localAssetManager);

		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = Bitmap
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		int[] pixels = new int[width * height];
		int[] pixels2 = new int[localBitmap2.getWidth()
				* localBitmap2.getHeight()];
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleOldPhoto(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
		Bitmap map = Bitmap
				.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		map.setPixels(pixels, 0, width, 0, 0, width, height);

		im.setImageBitmap(map);
	}

	public void Lomo() {
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("colorful/2.jpg",
				localAssetManager);

		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = Bitmap
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		int[] pixels = new int[width * height];
		int[] pixels2 = new int[localBitmap2.getWidth()
				* localBitmap2.getHeight()];
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoSnow(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
		Bitmap map = Bitmap
				.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		map.setPixels(pixels, 0, width, 0, 0, width, height);

		im.setImageBitmap(map);
	}
	public void LomoHaolo() {
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("colorful/mtxx_qcgy.jpg",
				localAssetManager);

		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = Bitmap
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		int[] pixels = new int[width * height];
		int[] pixels2 = new int[localBitmap2.getWidth()
				* localBitmap2.getHeight()];
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoHalo(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
		Bitmap map = Bitmap
				.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		map.setPixels(pixels, 0, width, 0, 0, width, height);

		im.setImageBitmap(map);
	}
	
	
	public void LomoHDR(){
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		int[] pixels = new int[width * height];
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoHDR(pixels, width, height);
		bmp.setPixels(pixels, 0, width, 0, 0, width, height);
		im.setImageBitmap(bmp);
	}
	
	public void LomoB(){
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		int[] pixels = new int[width * height];
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoB(pixels, width, height);
		bmp.setPixels(pixels, 0, width, 0, 0, width, height);
		im.setImageBitmap(bmp);
	}
	public Bitmap AssertFile(String paramString, AssetManager paramAssetManager) {
		InputStream localInputStream;
		Bitmap localObject3 = null;
		try {

			new StringBuilder("LoadAssertsPic path=").append(paramString)
					.toString();
			localInputStream = paramAssetManager.open(paramString);
			localObject3 = BitmapFactory.decodeStream(localInputStream, null,
					new BitmapFactory.Options());
		} catch (IOException localIOException2) {

		}
		return localObject3;
	}
}
